﻿using System;
using System.Linq;
using System.Windows.Forms;
using System.Text;
using System.Data;

namespace Smart
{
    public partial class frmTransfer : Form
    {
        public frmTransfer()
        {
            InitializeComponent();
            tabControl1.SelectedIndex = 0;
            button2.Click += new EventHandler(button2_Click);
        }

        void button2_Click(object sender, EventArgs e)
        {
            DialogResult dlg = MessageBox.Show("Evrağı Silmek istediğinize eminmisiniz ?", "Uyarı", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (dlg == DialogResult.Yes)
            {
                for (int row = 1; row < gridView1.Rows.Count; row++)
                {
                    if (Cevir.Parse<bool>(gridView1[row, 0]))
                    {
                        SayimTerminalSil(Convert.ToDateTime(gridView1[row, 1]), Cevir.Parse<int>(gridView1[row, 4]), Cevir.Parse<int>(gridView1[row, 5]));
                        gridView1.Rows.Remove(row);
                    }
                }
            }
            MessageBox.Show("Tamamlandı");
        }

        private void frmTransfer_Load(object sender, EventArgs e)
        {
            using (Query q = new Query(ProgramTipleri.SqlCe))
            {
                q.Add("select dep_kodu,depo_adi from TBL_DEPO");
                var liste = q.ToList(new { Kodu = "", Adi = "" });
                cmbDepo.DataSource = liste;
                cmbDepo.DisplayMember = "Adi";
                cmbDepo.ValueMember = "Kodu";
            }
        }

        private void btnGeri_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SayimGonder(DateTime tarih, int evrakNo, int depoNo)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT     say.sym_tarih AS tarih, TBL_DEPO.dep_kodu AS depo, say.sym_evrakno AS evrak_no, TBL_STOK.stk_kod AS stokkod,say.sym_miktar AS miktar, TBL_BARKOD.bk_barkod AS barkod ");
            sb.Append(" FROM         TBL_DEPO INNER JOIN TBL_SAYIM as say ON TBL_DEPO.rec_id = say.sym_depoid INNER JOIN TBL_STOK ON say.sym_stokid = TBL_STOK.rec_id LEFT OUTER JOIN TBL_BARKOD ON say.sym_barkodid = TBL_BARKOD.rec_id ");
            sb.AppendFormat("WHERE     (say.sym_tarih = '{0}') AND (TBL_DEPO.dep_kodu = '{1}') AND (say.sym_evrakno = {2}) ", tarih.ToSqlString(), depoNo, evrakNo);
            DataTable dt = SqlCeClient.Get.Fill(sb.ToString());
            BackOffice.Get.SayimAktar(dt, true);
        }

        private void SayimTerminalSil(DateTime tarih, int evrakNo, int depoNo)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("DELETE FROM TBL_SAYIM WHERE  (sym_tarih = '{0}') AND (sym_depoid = {1}) AND (sym_evrakno = {2}) ", tarih.ToSqlString(), depoNo, evrakNo);
            SqlCeClient.Get.Exec(sb.ToString());
        }

        private void SayimAktarildiCheck()
        {
            SqlCeClient.Get.Exec("UPDATE    TBL_SAYIM SET              sym_gonderildi = 1 WHERE     (sym_gonderildi = 0)");
        }

        private void SayimGonder()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT     say.sym_tarih AS tarih, TBL_DEPO.dep_kodu AS depo, say.sym_evrakno AS evrak_no, TBL_STOK.stk_kod AS stokkod,say.sym_miktar AS miktar, TBL_BARKOD.bk_barkod AS barkod ");
            sb.Append(" FROM         TBL_DEPO INNER JOIN TBL_SAYIM as say ON TBL_DEPO.rec_id = say.sym_depoid INNER JOIN TBL_STOK ON say.sym_stokid = TBL_STOK.rec_id LEFT OUTER JOIN TBL_BARKOD ON say.sym_barkodid = TBL_BARKOD.rec_id ");
            sb.Append(" WHERE     (say.sym_gonderildi = 0) ");
            sb.Append(" order by say.sym_tarih,TBL_DEPO.dep_kodu, say.sym_evrakno");
            DataTable dt = SqlCeClient.Get.Fill(sb.ToString());
            if (BackOffice.Get.SayimAktar(dt, false))
                SayimAktarildiCheck();
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 1)
            {
                DataTable dt = SqlCeClient.Get.Fill("SELECT     TBL_SAYIM.sym_tarih AS Tarih, TBL_DEPO.dep_kodu AS [Depo No], TBL_DEPO.depo_adi AS [Depo Adı], TBL_SAYIM.sym_evrakno AS [Evrak No],TBL_SAYIM.sym_depoid AS depoid FROM TBL_SAYIM INNER JOIN  TBL_DEPO ON TBL_SAYIM.sym_depoid = TBL_DEPO.rec_id group by TBL_SAYIM.sym_tarih, TBL_DEPO.dep_kodu, TBL_DEPO.depo_adi, TBL_SAYIM.sym_evrakno,TBL_SAYIM.sym_depoid");
                dt.Columns.Add("Seç", typeof(bool));
                dt.Columns["Seç"].SetOrdinal(0);
                gridView1.Cols.Fixed = 0;
                gridView1.DataSource = dt;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult dlg = MessageBox.Show("Tüm evrağı yeniden göndermeyi sectiniz! evet'i seçerseniz mikrodaki evrak silinecek ve terminaldeki haliyle kaydedilecektir. Eminmisiniz ?", "Uyarı", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (dlg == DialogResult.Yes)
            {
                for (int row = 1; row < gridView1.Rows.Count; row++)
                {
                    if (Cevir.Parse<bool>(gridView1[row, 0]))
                        SayimGonder(Convert.ToDateTime(gridView1[row, 1]), Cevir.Parse<int>(gridView1[row, 4]), Cevir.Parse<int>(gridView1[row, 2]));
                }
            }
            MessageBox.Show("Tamamlandı");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (rdStokBarkod.Checked)
            {
                lblStatus.Text = "Stoklar aktarılıyor..";
                using (DbStok stok = new DbStok())
                {
                    foreach (string item in stok.BulkImport(new DateTime(1900, 1, 1)))
                    {
                        lblStatus.Text = item;
                        lblStatus.Refresh();
                    }
                }
                lblStatus.Text = "Barkodlar aktarılıyor..";
                using (DbBarkod barkod = new DbBarkod())
                {
                    foreach (string item in barkod.BulkImport(new DateTime(1900, 1, 1)))
                    {
                        lblStatus.Text = item;
                        lblStatus.Refresh();
                    }
                }

                MessageBox.Show("Tamamlandı");
            }
            else if (rdDepo.Checked)
            {
                using (DbDepo depo = new DbDepo())
                {
                    foreach (string item in depo.BulkImport(0))
                    {
                        lblStatus.Text = item;
                        lblStatus.Refresh();
                    }
                }
                using (DbSrmMerkezi srm = new DbSrmMerkezi())
                {
                    foreach (string item in srm.BulkImport())
                    {
                        lblStatus.Text = item;
                        lblStatus.Refresh();
                    }
                }
                MessageBox.Show("Tamamlandı");
            }
            else if (rdEnvanter.Checked)
            {
                if (cmbDepo.SelectedIndex > -1)
                {
                    using (DbEnvanter env = new DbEnvanter())
                    {
                        foreach (string item in env.BulkImport(Convert.ToInt32(cmbDepo.SelectedValue)))
                        {

                        }
                    }
                    MessageBox.Show("Tamamlandı");
                }
            }
            else if (rdMusteri.Checked)
            {
                using (DbCari cari = new DbCari())
                {
                    foreach (string item in cari.BulkImport())
                    {
                        lblStatus.Text = item;
                        lblStatus.Refresh();
                    }
                }
                using (DbCariAdres adres = new DbCariAdres())
                {
                    foreach (string item in adres.BulkImport())
                    {
                        lblStatus.Text = item;
                        lblStatus.Refresh();
                    }
                }
                MessageBox.Show("Tamamlandı");
            }
            else if (rdCariBakiye.Checked)
            {
                using (DbCariBakiye bakiye = new DbCariBakiye())
                {
                    foreach (string item in bakiye.BulkImport())
                    {
                        lblStatus.Text = item;
                        lblStatus.Refresh();
                    }
                }
                MessageBox.Show("Tamamlandı");
            }
            else if (rdSayim.Checked)
            {
                SayimGonder();
                MessageBox.Show("Tamamlandı");
            }
            else if (rdIskonto.Checked)
            {
                lblStatus.Text = "İsk. Matrisleri aktarılıyor..";
                using (DbMatris isk = new DbMatris())
                {
                    foreach (string item in isk.BulkImport())
                    {
                        lblStatus.Text = item;
                        lblStatus.Refresh();
                    }
                }

                MessageBox.Show("Tamamlandı");
            }
        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {

        }
    }
}